package by.scsbsu.database.dao;

import by.scsbsu.database.bean.Mark;
import by.scsbsu.database.tools.DbConnectionPool;

public class MarkDAO extends AbstractDAO {
	public static final String SQL_MARK_CREATE_MARK = "INSERT INTO mark ( mark , id_task_attach , id_label ) VALUES (?, ?, ?);";
	public static final String SQL_MARK_GET_ID_CREATE_MARK = "SELECT id FROM mark WHERE ( (id_task_attach = ?) AND (id_label = ?) );";
	//public static final String SQL_MARK_GET_MARK_BY_ID_TASK_ATTACH_ID_LABEL = "SELECT mark FROM mark WHERE ( (id_task_attach = ?) AND (id_label = ?) );";
	public static final String SQL_MARK_GET_MARK_BY_ID = "SELECT mark FROM mark WHERE ( id = ? );";
	public static final String SQL_MARK_DELETE_MARK_BY_ID = "DELETE FROM mark WHERE ( id = ? );";
	//public static final String SQL_MARK_UPDATE_MARK = "UPDATE mark SET ( mark = ? ) where ( id = ? );";
	//public static final String SQL_MARK_DELETE_MARK_BY_ID_TASK_ATTACH_ID_LABEL = "DELETE FROM mark WHERE ( (id_task_attach = ?) AND (id_label = ?) );";
	
	
	public Mark createMark(Mark mark){
		try {
			connection = DbConnectionPool.getConnection();
			
			statement = connection.prepareStatement(SQL_MARK_CREATE_MARK);
			statement.setInt(1, mark.getMark());
			statement.setInt(2, mark.getIdTaskAttach());
			statement.setInt(3, mark.getIdLabel());
			statement.executeUpdate();
			
			statement = connection.prepareStatement(SQL_MARK_GET_ID_CREATE_MARK);
			statement.setInt(1, mark.getIdTaskAttach());
			statement.setInt(2, mark.getIdLabel());
			resultSet = statement.executeQuery();
			
			if (resultSet.next()) {
				mark.setId(resultSet.getInt("id"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close();
		}
		return mark;
	}
	
	
	public int getMarkById(int id){
		int mark = 0;
		try {
			connection = DbConnectionPool.getConnection();
			
			statement = connection.prepareStatement(SQL_MARK_GET_MARK_BY_ID);
			statement.setInt(1, id);
			
			resultSet = statement.executeQuery();
			if(resultSet.next()){
				mark = resultSet.getInt("mark");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close();
		}
		return mark;
	}
	
	public void deleteMark(int id){
		try {
			connection = DbConnectionPool.getConnection();
			
			statement = connection.prepareStatement(SQL_MARK_DELETE_MARK_BY_ID);
			statement.setInt(1, id);
			statement.executeUpdate();			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close();
		}
	}
	
	/*public int getMark(int id_task_attach, int id_label){
		int mark = 0;
		try {
			connection = DbConnectionPool.getConnection();
			
			statement = connection.prepareStatement(SQL_MARK_GET_MARK_BY_ID_TASK_ATTACH_ID_LABEL);
			statement.setInt(1, id_task_attach);
			statement.setInt(2, id_label);
			
			resultSet = statement.executeQuery();
			if(resultSet.next()){
				mark = resultSet.getInt("mark");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close();
		}
		return mark;
	}*/
	
	/*public void deleteMark(int id_task_attach, int id_label){
		try {
			connection = DbConnectionPool.getConnection();
			
			statement = connection.prepareStatement(SQL_MARK_DELETE_MARK_BY_ID_TASK_ATTACH_ID_LABEL);
			statement.setInt(1, id_task_attach);
			statement.setInt(2, id_label);
			statement.executeUpdate();			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			close();
		}
	}*/
}
